package top.saodisheng;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;

/**
 * description:
 *
 * @author saodisheng
 * @date 2022/8/1
 */
public class MybatisPlusGenerator {

    private static final String DB_URL = "jdbc:mysql://{{databaseUrl}}/{{databaseName}}?allowMultiQueries=true&autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai";
    private static final String USERNAME = "userName";
    private static final String PASSWORD = "password";

    public static void main(String[] args) {
        String moduleName = "D:/workspace/codes/gis-egov-core-community-portal/community-portal-server/portal-server-provider";
        String rootPackage = "com.sf.gis";
        String serviceName = "ua";
        String removePrefix = "";
        genCode(moduleName, rootPackage, serviceName, removePrefix, "scomm_fc_training_user");
    }

    private static void genCode(String projectDir, String rootPackage, String moduleName,
                                String removePrefix, String... tables) {
        String javaFileDir = projectDir + "/src/main/java";
        String xmlFileDir = projectDir + "/src/main/resources/mapper";

        FastAutoGenerator.create(DB_URL, USERNAME, PASSWORD)
                .globalConfig(builder -> {
                    builder.author("01420175")
                            .enableSwagger()
                            .fileOverride()
                            .commentDate("yyyy-MM-dd")
                            .outputDir(javaFileDir);
                })
                .packageConfig(builder -> {
                    builder.parent(rootPackage)
                            .moduleName(moduleName)
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, xmlFileDir));
                })
                .strategyConfig(builder -> {
                    builder.addInclude(tables).addTablePrefix(removePrefix)
                            .entityBuilder().enableLombok().enableTableFieldAnnotation()
                            .mapperBuilder().enableMapperAnnotation()
                            .controllerBuilder().enableRestStyle()
                            .mapperBuilder().enableBaseResultMap().enableBaseColumnList();
                }).templateConfig(builder -> {
            builder.entity("/templates/entity.java")
                    .mapper("/templates/mapper.java")
                    .service("/templates/service.java")
                    .serviceImpl("/templates/serviceImpl.java")
                    .controller("/templates/controller.java")
                    .mapperXml("/templates/mapper.xml");
        })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
